package logic

import (
	"apple/applerpc/applerpc"
	"apple/applerpc/internal/svc"
	"apple/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerCetLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerCetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerCetLogic {
	return &PerCetLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善信息
func (l *PerCetLogic) PerCet(in *applerpc.PerCetRequest) (*applerpc.PerCetResponse, error) {
	// todo: add your logic here and delete this line
	//var user mysql.User
	//mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	//if user.ID == 0 {
	//	return nil, fmt.Errorf("账号不存在")
	//}
	//if user.Password != in.Password {
	//	return nil, fmt.Errorf("密码错误")
	//}
	//user.Account = in.Account
	//user.Tel = in.Tel
	//user.Password = in.Password
	//user.Name = in.Name
	//mysql.DB.Debug().Updates(&user)
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	if in.Password != user.Password {
		return nil, fmt.Errorf("密码错误")
	}
	user.Account = in.Account
	user.Tel = in.Tel
	user.Password = in.Password
	user.Name = in.Name
	mysql.DB.Debug().Updates(&user)
	return &applerpc.PerCetResponse{
		Id: int64(user.ID),
	}, nil
}
